/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [FileUtil.java]
 * Creation Date        [11-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_gui.notepad_V1.ui.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 * 文件工具类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class FileUtil {

	/**
	 * 打开文件
	 * 
	 * @param textArea
	 * @param file
	 */
	public void openFile(JTextArea textArea, File file) {
		FileReader reader = null;
		try {
			reader = new FileReader(file);
			int length = (int) file.length();
			char[] buffer = new char[length];
			reader.read(buffer, 0, length);
			textArea.setText(new String(buffer));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 保存文件
	 * 
	 * @param textArea
	 * @param isNewFile
	 * @param file
	 * @param statusLabel
	 */
	public void saveFile(JTextArea textArea, boolean isNewFile, File file,
			JLabel statusLabel) {
		textArea.requestFocusInWindow();
		if (isNewFile) { // 如果是未保存过的
			JFileChooser chooser = new JFileChooser();
			chooser.addChoosableFileFilter(new NotepadFileFilter("txt"));
			chooser.addChoosableFileFilter(new NotepadFileFilter("sql"));
			chooser.addChoosableFileFilter(new NotepadFileFilter("java"));
			chooser.setApproveButtonText("确定");
			chooser.setDialogTitle("保存为");

			int result = chooser.showSaveDialog(textArea.getParent());
			if (result == JFileChooser.CANCEL_OPTION) {
				statusLabel.setText("没有选择任何文件");
				return;
			}

			File saveFileName = chooser.getSelectedFile();
			if (saveFileName == null || saveFileName.getName().equals("")) {
				JOptionPane.showMessageDialog(textArea.getParent(), "不合法的文件名",
						"不合法的文件名", JOptionPane.ERROR_MESSAGE);
			} else {
				BufferedWriter writer = null;
				try {
					writer = new BufferedWriter(new FileWriter(saveFileName));
					writer.write(textArea.getText(), 0, textArea.getText()
							.length());
					writer.flush();
					isNewFile = false;
					file = saveFileName;
					statusLabel.setText("当前打开文件:"
							+ saveFileName.getAbsoluteFile());
				} catch (IOException e) {
				} finally {
					if (writer != null) {
						try {
							writer.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		} else { // 如果是已保存过的
			BufferedWriter writer = null;
			try {
				writer = new BufferedWriter(new FileWriter(file));
				int length = textArea.getText().length();
				writer.write(textArea.getText(), 0, length);
				writer.flush();
			} catch (IOException e) {
			} finally {
				if (writer != null) {
					try {
						writer.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
